package com.vancouver.knows.business.tracker;

import com.pangu.task.IResultReceiver;
import com.pangu.task.OperateResult;
import com.vancouver.knows.business.BSharedPrefManager;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.Constants;
import com.vancouver.knows.business.bean.UserInfo;
/**
 * 获取用户信息列表
 *
 * @author llf
 *
 */
public class UserInfoTracker extends ABInvokeTracker {

	public UserInfoTracker(BusinessModule module, IResultReceiver resultReceiver) {
		super(module, resultReceiver);
	}

	@Override
	public String TAG() {
		return UserInfoTracker.class.getSimpleName();
	}

	@Override
	public void handleResult(OperateResult result) {
		saveUserInfo(result);
		imContext.handleMobEmptyMessage(Constants.BUSINESS_STATUS_LOGIN_SUCCESS);

	}

	private void saveUserInfo(OperateResult result) {
		UserInfo userInfo = (UserInfo) result.getResultData();
		if (userInfo == null)
			return;
		BSharedPrefManager sharedPrefManager = new BSharedPrefManager(imContext);
		sharedPrefManager.saveUserId(userInfo.getUserID());
		sharedPrefManager.saveUserName(userInfo.getName());
		sharedPrefManager.saveUserEmail(userInfo.getEmail());
		if (userInfo.getAvatar() == null) {
			sharedPrefManager.saveAvatar(userInfo.getUserMeta().getAvatar());
		} else {
			sharedPrefManager.saveAvatar(userInfo.getAvatar());
		}
		sharedPrefManager.saveLogin(userInfo.getLogin());
		sharedPrefManager.saveTotalUnReadComment(userInfo.getTotalUnReadComment());
		sharedPrefManager.saveTotalUnReadPostComment(userInfo.getTotalUnReadPostComment());
		//积分明细
		sharedPrefManager.saveTotalPoints(userInfo.userPointsDetailsInfo.getTotalPoints());
		sharedPrefManager.savePostPoints(userInfo.userPointsDetailsInfo.getPostPoints());
		sharedPrefManager.saveUpvotePoints(userInfo.userPointsDetailsInfo.getUpvotePoints());
		sharedPrefManager.saveCommentPoints(userInfo.userPointsDetailsInfo.getCommentPoints());
		sharedPrefManager.savePostUpvotePoints(userInfo.userPointsDetailsInfo.getPostUpvotePoints());
		sharedPrefManager.saveCommentUpvotePoints(userInfo.userPointsDetailsInfo.getCommentUpvotePoints());
		sharedPrefManager.saveRedeemedPoints(userInfo.userPointsDetailsInfo.getRedeemedPoints());
		sharedPrefManager.savePointsPolicy(userInfo.userPointsDetailsInfo.getPointsPolicy());
		sharedPrefManager.savePointsStore(userInfo.userPointsDetailsInfo.getPointsStore());
		sharedPrefManager.saveUserMeta(userInfo.getUserMeta());
		sharedPrefManager.saveRegisterType(userInfo.getRegisterType());
		module.getCacheManager().setUserInfo(userInfo);
		module.getTaskMarkPool().getFetchEventsTaskMark().reinitTaskMark();

	}

}
